//
//  NSString+Extend.m
//  AFNetworking
//
//  Created by v on 2020/11/6.
//

#import "NSString+Extend.h"
#import <AFNetworking/AFNetworking.h>

@implementation NSString (Extend)

- (NSString *)lanB_baseUrl
{
    NSString *result = self;
    NSRange range = [self rangeOfString:@"?"];
    if (range.location != NSNotFound) {
        result = [self substringToIndex:range.location];
    }
    return result;
}

- (NSString *)lanB_query
{
    NSString *result = self;
    NSRange range = [self rangeOfString:@"?"];
    if (range.location != NSNotFound) {
        result = [self substringFromIndex:range.location+range.length];
    }
    return result;
}

- (NSString *)lanB_queryWithDictionary:(NSDictionary *)parameters
{
    NSString *result = self;
    NSString *queryStr = AFQueryStringFromParameters(parameters);
    if ([self rangeOfString:@"?"].location == NSNotFound) {
        result = [NSString stringWithFormat:@"%@?%@", self, queryStr];
    }else{
        result = [NSString stringWithFormat:@"%@&%@", self, queryStr];
    }
    return result;
}

- (NSDictionary *)lanB_queryDictionary
{
    __block NSMutableDictionary *result = nil;
    NSString *queryStr = self.lanB_query;
    NSArray  *aryQuery = [queryStr componentsSeparatedByString:@"&"];
    [aryQuery enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *subTemp = [obj componentsSeparatedByString:@"="];
        if (subTemp.count == 2) {
            if (!result) result = [NSMutableDictionary new];
            result[subTemp[0]] = subTemp[1];
        }
    }];
    return result;
}

@end
